﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using TaskManagerApp.Common;
using TaskManagerApp.Model;

namespace TaskManagerApp.ViewModel
{
    public class AllTasksListViewModel : WorkspaceViewModel
    {
        #region Fields

        readonly ITaskRepository taskRepository;

        RelayCommand editCommand;
        RelayCommand deleteCommand;

        #endregion // Fields

        /// <summary>
        /// Returns a command that deletes the Task.
        /// </summary>
        public ICommand EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new RelayCommand(
                        param => this.Edit(),
                        param => this.CanEdit
                        );
                }
                return editCommand;
            }
        }

        private bool CanEdit
        {
            get { return true; }
        }

        private void Edit()
        {
            
        }

        /// <summary>
        /// Returns a command that deletes the Task.
        /// </summary>
        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(
                        param => this.Delete(),
                        param => this.CanDelete
                        );
                }
                return deleteCommand;
            }
        }

        private bool CanDelete
        {
            get
            {
                if (AllTasks.Count > 0)
                    return true;

                return false;
            }
        }

        private void Delete()
        {
            AllTasks.Remove(AllTasks[0]);
        }


        public AllTasksListViewModel(ITaskRepository taskRepository)
        {
            if (taskRepository == null)
                throw new ArgumentNullException("taskRepository");

            this.taskRepository = taskRepository;

            base.DisplayName = Strings.AllTasksViewModel_DisplayName;

            taskRepository.taskAdded += taskRepository_taskAdded;
            taskRepository.taskRemoved += taskRepository_taskRemoved;

            // Populate the AllTasks collection with CustomerViewModels.
            this.CreateAllTasks();       
        }

        void taskRepository_taskRemoved(object sender, TaskEventArgs e)
        {

            var viewModel = new TaskViewModel(e.NewTask, taskRepository);
            this.AllTasks.Remove(viewModel);
        }

        private void CreateAllTasks()
        {

            List<TaskViewModel> all =
                (from task in taskRepository.GetTasks()
                 select new TaskViewModel(task, taskRepository)).ToList();

            foreach (TaskViewModel cvm in all)
                cvm.PropertyChanged += cvm_PropertyChanged;

            this.AllTasks = new ObservableCollection<TaskViewModel>(all);
            this.AllTasks.CollectionChanged += AllTasks_CollectionChanged;

        }

        void AllTasks_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (TaskViewModel taskVM in e.NewItems)
                    taskVM.PropertyChanged += this.cvm_PropertyChanged;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (TaskViewModel taskVM in e.OldItems)
                    taskVM.PropertyChanged += this.cvm_PropertyChanged;
        }

        void cvm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //any validation on the property needs to be done! do it here..

            string IsSelected = "IsSelected";

            // Make sure that the property name we're referencing is valid.
            // This is a debugging technique, and does not execute in a Release build.
            (sender as TaskViewModel).VerifyPropertyName(IsSelected);

            // When a task is selected or unselected, we must let the
            // world know that the property has changed,
            // so that it will be queried again for a new value.
            if (e.PropertyName == IsSelected)
                this.OnPropertyChanged("IsSelected");
        }

        void taskRepository_taskAdded(object sender, TaskEventArgs e)
        {
            var viewModel = new TaskViewModel(e.NewTask, taskRepository);
            this.AllTasks.Add(viewModel);
        }

        public ObservableCollection<TaskViewModel> AllTasks { get; private set; }
    }
}
